using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace EncodePDU
{
	public class frmEncoder : System.Windows.Forms.Form
	{
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;
		private System.Windows.Forms.TextBox txtNumber;
		private System.Windows.Forms.Label lblNumber;
		private System.Windows.Forms.Label lblMessage;
		private System.Windows.Forms.TextBox txtMessage;
		private System.Windows.Forms.TextBox txtEncoded;
		private System.Windows.Forms.Label lblEncoded;
		private System.Windows.Forms.Label lblAct;
		private System.Windows.Forms.Label lblActualLength;
		private System.Windows.Forms.Label lblSmsc;
		private System.Windows.Forms.TextBox txtSmsc;
		private System.Windows.Forms.CheckBox chkFromDevice;

		public frmEncoder()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose (bool disposing)
		{
			if (disposing)
			{
				if (components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose(disposing);
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.txtNumber = new System.Windows.Forms.TextBox();
			this.lblNumber = new System.Windows.Forms.Label();
			this.lblMessage = new System.Windows.Forms.Label();
			this.txtMessage = new System.Windows.Forms.TextBox();
			this.txtEncoded = new System.Windows.Forms.TextBox();
			this.lblEncoded = new System.Windows.Forms.Label();
			this.lblAct = new System.Windows.Forms.Label();
			this.lblActualLength = new System.Windows.Forms.Label();
			this.lblSmsc = new System.Windows.Forms.Label();
			this.txtSmsc = new System.Windows.Forms.TextBox();
			this.chkFromDevice = new System.Windows.Forms.CheckBox();
			this.SuspendLayout();
			// 
			// txtNumber
			// 
			this.txtNumber.Location = new System.Drawing.Point(80, 56);
			this.txtNumber.Name = "txtNumber";
			this.txtNumber.Size = new System.Drawing.Size(320, 20);
			this.txtNumber.TabIndex = 1;
			this.txtNumber.Text = "";
			this.txtNumber.TextChanged += new System.EventHandler(this.txtNumber_TextChanged);
			// 
			// lblNumber
			// 
			this.lblNumber.Location = new System.Drawing.Point(8, 56);
			this.lblNumber.Name = "lblNumber";
			this.lblNumber.Size = new System.Drawing.Size(64, 23);
			this.lblNumber.TabIndex = 0;
			this.lblNumber.Text = "Number:";
			this.lblNumber.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
			// 
			// lblMessage
			// 
			this.lblMessage.Location = new System.Drawing.Point(8, 88);
			this.lblMessage.Name = "lblMessage";
			this.lblMessage.Size = new System.Drawing.Size(64, 23);
			this.lblMessage.TabIndex = 2;
			this.lblMessage.Text = "Message:";
			// 
			// txtMessage
			// 
			this.txtMessage.Location = new System.Drawing.Point(80, 88);
			this.txtMessage.Multiline = true;
			this.txtMessage.Name = "txtMessage";
			this.txtMessage.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
			this.txtMessage.Size = new System.Drawing.Size(320, 88);
			this.txtMessage.TabIndex = 3;
			this.txtMessage.Text = "";
			this.txtMessage.TextChanged += new System.EventHandler(this.txtMessage_TextChanged);
			// 
			// txtEncoded
			// 
			this.txtEncoded.Location = new System.Drawing.Point(80, 184);
			this.txtEncoded.Multiline = true;
			this.txtEncoded.Name = "txtEncoded";
			this.txtEncoded.ReadOnly = true;
			this.txtEncoded.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
			this.txtEncoded.Size = new System.Drawing.Size(320, 104);
			this.txtEncoded.TabIndex = 5;
			this.txtEncoded.Text = "";
			// 
			// lblEncoded
			// 
			this.lblEncoded.Location = new System.Drawing.Point(8, 184);
			this.lblEncoded.Name = "lblEncoded";
			this.lblEncoded.Size = new System.Drawing.Size(64, 40);
			this.lblEncoded.TabIndex = 4;
			this.lblEncoded.Text = "Encoded Message:";
			// 
			// lblAct
			// 
			this.lblAct.Location = new System.Drawing.Point(80, 296);
			this.lblAct.Name = "lblAct";
			this.lblAct.Size = new System.Drawing.Size(152, 23);
			this.lblAct.TabIndex = 7;
			this.lblAct.Text = "Actual PDU length (bytes):";
			// 
			// lblActualLength
			// 
			this.lblActualLength.Location = new System.Drawing.Point(224, 296);
			this.lblActualLength.Name = "lblActualLength";
			this.lblActualLength.Size = new System.Drawing.Size(72, 23);
			this.lblActualLength.TabIndex = 8;
			// 
			// lblSmsc
			// 
			this.lblSmsc.Location = new System.Drawing.Point(8, 24);
			this.lblSmsc.Name = "lblSmsc";
			this.lblSmsc.Size = new System.Drawing.Size(64, 23);
			this.lblSmsc.TabIndex = 6;
			this.lblSmsc.Text = "Service center:";
			this.lblSmsc.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
			// 
			// txtSmsc
			// 
			this.txtSmsc.Location = new System.Drawing.Point(80, 24);
			this.txtSmsc.Name = "txtSmsc";
			this.txtSmsc.Size = new System.Drawing.Size(200, 20);
			this.txtSmsc.TabIndex = 7;
			this.txtSmsc.Text = "";
			this.txtSmsc.TextChanged += new System.EventHandler(this.txtSmsc_TextChanged);
			// 
			// chkFromDevice
			// 
			this.chkFromDevice.Location = new System.Drawing.Point(296, 24);
			this.chkFromDevice.Name = "chkFromDevice";
			this.chkFromDevice.TabIndex = 8;
			this.chkFromDevice.Text = "Get from device";
			this.chkFromDevice.CheckedChanged += new System.EventHandler(this.chkFromDevice_CheckedChanged);
			// 
			// frmEncoder
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(416, 336);
			this.Controls.Add(this.chkFromDevice);
			this.Controls.Add(this.lblSmsc);
			this.Controls.Add(this.txtSmsc);
			this.Controls.Add(this.lblActualLength);
			this.Controls.Add(this.lblAct);
			this.Controls.Add(this.lblEncoded);
			this.Controls.Add(this.txtEncoded);
			this.Controls.Add(this.txtMessage);
			this.Controls.Add(this.lblMessage);
			this.Controls.Add(this.lblNumber);
			this.Controls.Add(this.txtNumber);
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
			this.MaximizeBox = false;
			this.Name = "frmEncoder";
			this.Text = "SMS PDU Encoder";
			this.Load += new System.EventHandler(this.frmEncoder_Load);
			this.ResumeLayout(false);
		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main()
		{
			Application.Run(new frmEncoder());
		}

		private void Encode()
		{
			string smsc = (chkFromDevice.Checked ? "" : txtSmsc.Text);
			try
			{
				int actualLength;
				txtEncoded.Text = SubmitPdu.GetPdu(txtNumber.Text, txtMessage.Text, smsc, out actualLength);
				lblActualLength.Text = actualLength.ToString();
			}
			catch(Exception ex)
			{
				txtEncoded.Text = "[Error: " + ex.Message + " (" + ex.GetType().ToString() + ")]";
				lblActualLength.Text = "0";
			}
		}

		private void txtMessage_TextChanged(object sender, System.EventArgs e)
		{
			Encode();
		}

		private void txtNumber_TextChanged(object sender, System.EventArgs e)
		{
			Encode();
		}
		
		private void chkFromDevice_CheckedChanged(object sender, System.EventArgs e)
		{
			txtSmsc.Enabled = !chkFromDevice.Checked;
			Encode();
		}
		
		private void txtSmsc_TextChanged(object sender, System.EventArgs e)
		{
			Encode();
		}
		
		private void frmEncoder_Load(object sender, System.EventArgs e)
		{
			chkFromDevice.Checked = true;
		}
	}
}
